/**
 * Generali Solutions d'assurances - Tous droits réservés &copy; 2007 - 2010
 */
package fr.generali.accueilclient.opt.transco.services.impl;

import java.util.List;

import javax.xml.ws.WebServiceException;

import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import fr.generali.accueilclient.commun.exception.AccueilClientBusinessException;
import fr.generali.accueilclient.commun.exception.AccueilClientTechnicalException;
import fr.generali.accueilclient.opt.transco.om.ITransco;
import fr.generali.accueilclient.opt.transco.om.ITypeTransco;
import fr.generali.accueilclient.opt.transco.services.IListeTranscoService;
import fr.generali.relationclient.marketing.opt.detailtransco.DtoTransco;
import fr.generali.relationclient.marketing.opt.detailtransco.services.IListeTranscoWebService;
import fr.generali.relationclient.marketing.opt.detailtransco.services.TranscoWSBusinessException;
import fr.generali.relationclient.marketing.opt.detailtransco.services.TranscoWSTechnicalException;

/**
 * @author Holmes Kelly
 */

@Service("listeTranscoService")
public class ListeTranscoService implements IListeTranscoService {

    /**
     * LOGGER
     */
    private static final Logger LOGGER = LoggerFactory.getLogger(ListeTranscoService.class);

    /**
     * Appel mapper contenant les assembler
     */
    @Autowired
    @Qualifier("listeTranscoMapper")
    private ListeTranscoMapper listeTranscoMapper;

    /**
     * Appel web service ListeTranscoWebService
     */
    @Autowired
    @Qualifier("listeTranscoWebService")
    private IListeTranscoWebService listeTranscoWebService;

    /**
     * {@inheritDoc}
     */
    public List<ITransco> consulterListTransco(ITypeTransco typeTransco) throws AccueilClientBusinessException {

        List<ITransco> listTransco = null;

        if (typeTransco != null) {

            try {
                final List<DtoTransco> listDtoTransco =
                                listeTranscoWebService.consulterLibelle(listeTranscoMapper
                                                .mapperTypeTransco2DtoTypeTransco(typeTransco));

                if (CollectionUtils.isNotEmpty(listDtoTransco)) {

                    listTransco = listeTranscoMapper.mapperListerTransco2ListITransco(listDtoTransco);
                }

            } catch (TranscoWSBusinessException e) {

                LOGGER.debug(e.getMessage());

                throw new AccueilClientBusinessException(e.getMessage(), e);

            } catch (TranscoWSTechnicalException e) {

                LOGGER.debug(e.getMessage());

                throw new AccueilClientBusinessException(e.getMessage(), e);

            } catch (WebServiceException e) {

                LOGGER.debug(e.getMessage());

                throw new AccueilClientTechnicalException(e.getMessage(), e);
            }
        }

        return listTransco;
    }

}
